/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package giuliani_mapelli_marocco.business;

import giuliani_mapelli_marocco.entity.Event;
import giuliani_mapelli_marocco.enumerations.WeatherMessageType;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.persistence.NoResultException;

@Startup
@Singleton
public class WeatherTimer {
    
    @EJB
    private WeatherManager wm;
    @EJB
    private EventManager em;
    @EJB
    private WeatherNotificationManager wnm;
    
    private TimeTools time = new TimeTools();
    
    
    /**
    *This method is called every 12 hours to update weatherinfo and create the consequential weather notification for outdoor events
    * to the user
    */
    @Schedule(second = "1", minute = "1", hour = "0,12", persistent = false)
    public void callForWeatherUpdate() {
         
        wm.updateAllWeatherforecast();
        
    }
 
    /**
     * This method will be lauched every midnight to create the notification for the participant of some event the day after
     * with bad weather condition
     */
    @Schedule(second = "1", minute = "3", hour = "0", persistent=false)
    public void callForBadWeatherParticipantNotification()  {
        
        
        Date currentDate = time.today();
        Date tomorrow = time.addDays(currentDate, 1);
         try{
        List<Event> events = em.findOutdoorEventByStartingDate(tomorrow);
         for(Event e: events) {
     
             if(wnm.isEventBadMultiDay(e))
                 wnm.createNotificationWeather(e, WeatherMessageType.PARTICIPANT_NOTIFICATION);
         }
         } catch(NoResultException ex) {
           
         }   
    }
   
    /**
     * This method will be fired every midnight to create weather notification to organizer of some outdoor event that three days after
     * has bad weather condition
     */
   @Schedule(second = "1", minute = "4", hour = "0", persistent=false)
   public void callForBadWeatherOrganizerNotification() {
        
 
         Date currentDate = time.today();
        Date ThreeDaysFromNow = time.addDays(currentDate, 3);
        try{
        List<Event> events = em.findOutdoorEventByStartingDate(ThreeDaysFromNow);
         for(Event e: events) {

             if(wnm.isEventBadMultiDay(e))
                 wnm.createNotificationWeather(e, WeatherMessageType.ORGANIZER_NOTIFICATION);
         }
         } catch(NoResultException ex) {
 
         }   
    }
   
    /**
     *starts automatically everyday at 1 am and deletes all the weather forecast of the day before 
     */
    @Schedule(second = "0", minute = "0", hour = "1", persistent=false)
   public void deleteOldWeatherForecast() {
       
       Date currentDate = time.today();
       Date yesterday = TimeTools.addDays(currentDate, -1);
       
       wm.deleteWeatherForecastByDate(yesterday);
       
   }
}
